#!/usr/local/BLBIN/bin/php
<?php
function get_request($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch);
	return trim($response);
}

function check_license_expire()
{
	global $key;

	if (!is_dir('/usr/share/goba')) {
		system('mkdir -p \'/usr/share/goba\' &> /dev/null');
	}

	$private = '-----BEGIN PRIVATE KEY-----' . "\r\n" . 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC5Hq/0a7g3fR9Y' . "\r\n" . '7IteOpM9FIgocmBa/V7TkURXdT+5y3OpVED/FGyQ+WoBWi5bym6CgTlp5bM6QvTe' . "\r\n" . 'QAoVNvDyRjxB4dENBhls/Gr9B1JBcuG6pm1Ruizhu8NsfL5hiCbyZGzSu7L7LKaz' . "\r\n" . '457RoJ01pH7p8IVKxJdW1vNsTiCLgTLgjePkO/hTQJMwO6MvXnpQ3ViI8kECThVS' . "\r\n" . 'lkhrz4UveenmJXBaop038MW2Y0VtVWFaDBdJ+HFN6tueKhIEGbgkB//TyGBuPAS4' . "\r\n" . 'XDe7MQVzg8OUz8NAFrWcJBmVUep8qiDJwVm2m5AvMiUHN4SVyk5wgVZ6XtGeiuOZ' . "\r\n" . 'MUfCQeM3AgMBAAECggEATiIKqAIzLZBtA65LIIlejeA1m8SysJTSSj+89aDyNawO' . "\r\n" . 'dECwcjkfnYz31fxHJszpGcILBoxBumx6R7gFyXB3//nvAuuZvYJ+hsZ/La7CZAdm' . "\r\n" . 'CM6nw1/uA5SpZge8nxPFRHdoiwVYBLWS4shGxzmgQPzd6EVxLN6O9cMh2OucxuQY' . "\r\n" . 'G8HgZ8akklpG8GRiYCesDdHVNycSbm+KrpkMgFVsVbZ2U3kKoti9D3XS24a5vSD0' . "\r\n" . 'kmbkftljpiMZOnHlGGLEIfWFO8ZeS27CvTK1mYgJEibdSTHAsqbH11lw7AUtkIdD' . "\r\n" . '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fgoI=' . "\r\n" . '-----END PRIVATE KEY-----';

	if (!file_exists('/usr/share/goba/.licensekeyjp')) {
		exec('wget -O /usr/share/goba/.licensekeyjp https://panel.licensepal.host/api/' . $key . '/getlicense > /dev/null 2>&1');
		$getdata = file_get_contents('/usr/share/goba/.licensekeyjp');
		$file = json_decode($getdata, true);
		$data = base64_decode($file['sig']);
		$decrypted = openssl_private_decrypt($data, $decrypted_1, $private);
		$json = json_decode($decrypted_1, true);
		$valid = $json['second'];
		$validdate = date('m/d/Y h:i:s a', time());
		$valid2 = strtotime($validdate);
	}
	else {
		$getdata = file_get_contents('/usr/share/goba/.licensekeyjp');
		$file = json_decode($getdata, true);
		$data = base64_decode($file['sig']);
		$decrypted = openssl_private_decrypt($data, $decrypted_1, $private);
		$json = json_decode($decrypted_1, true);
		$valid = $json['second'];
		$validdate = date('m/d/Y h:i:s a', time());
		$valid2 = strtotime($validdate);
	}

	if (0 <= $valid2 - $valid) {
		exec('wget -O /usr/share/goba/.licensekeyjp https://panel.licensepal.host/api/' . $key . '/getlicense > /dev/null 2>&1');
		$getdata = file_get_contents('/usr/share/goba/.licensekeyjp');
		$file = json_decode($getdata, true);
		$data = base64_decode($file['sig']);
		$decrypted = openssl_private_decrypt($data, $decrypted_1, $private);
		$json = json_decode($decrypted_1, true);
		$valid = $json['second'];
		$validdate = date('m/d/Y h:i:s a', time());
		$valid2 = strtotime($validdate);

		if (0 <= $valid2 - $valid) {
			disable();
			return true;
		}
	}

	return false;
}

function uninstall()
{
	disable();
	system('rm -rf   \'/usr/bin/gblicensejpv4\' &> /dev/null');
	system('rm -rf   \'/usr/bin/gblicensejpv4_run\' &> /dev/null');
	return true;
}

function remove_license()
{
	global $JetBackup;

	if (file_exists('/usr/share/goba/jetbackup/old/License.inc')) {
		system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
		system('mv \'/usr/share/goba/jetbackup/old/License.inc\'  \'' . $JetBackup . '/License.inc\' &> /dev/null');
	}
}

function enable()
{
	global $JetBackup;
	global $buyer;
	system('mkdir -p \'/usr/share/goba/jetbackup/old\' &> /dev/null');
	system('echo \'\' > \'/usr/share/goba/gblicensejpv4\' ');

	if (!file_exists('/usr/share/goba/jetbackup/old/License.inc')) {
		system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
		system('mv \'' . $JetBackup . '/License.inc\' \'/usr/share/goba/jetbackup/old/License.inc\'   &> /dev/null');
	}

	return true;
}

function disable()
{
	global $JetBackup;

	if (file_exists('/usr/share/goba/jetbackup/old/License.inc')) {
		system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
		system('mv \'/usr/share/goba/jetbackup/old/License.inc\'  \'' . $JetBackup . '/License.inc\' &> /dev/null');
	}

	system('rm -rf  "/usr/share/goba/gblicensejb"  &> /dev/null');
	system('rm -rf  /etc/cron.d/licensejpv4  &> /dev/null');
	return true;
}

function real_execute($cmd)
{
	$a = popen($cmd, 'r');

	while ($b = fgets($a, 2048)) {
		echo $b;
		ob_flush();
		flush();
	}

	pclose($a);
}

function exec_output($cmd)
{
	exec($cmd, $output, $return_var);

	if (1 < count($output)) {
		return join("\r\n", $output);
	}

	return $output[0];
}

function remove_trial()
{
	if (!preg_match('/PAGE\\.info\\.messages = PAGE\\.info\\.messages\\.filter/', file_get_contents('/usr/local/cpanel/whostmgr/docroot/templates/jetbackup/index.tmpl'))) {
		system('sed  -i -e "s|PAGE\\.info.*;|PAGE.info = [% info.success ? info.data.json() : "{}" %];PAGE.info.messages = PAGE.info.messages.filter(e => \\!/after the trial period/.test(e.message));|g" "/usr/local/cpanel/whostmgr/docroot/templates/jetbackup/index.tmpl" &> /dev/null');
	}
}

function get_http_response_code($domain1)
{
	$ch = curl_init($domain1);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	$output = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
}

function exec_license()
{
	global $status;
	global $JetBackup;
	global $buyer;
	system('mkdir -p \'/usr/share/goba/jetbackup/old\' &> /dev/null');
	system('echo \'\' > \'/usr/share/goba/gblicensejb\' ');

	if (!file_exists('/usr/share/goba/jetbackup/old/License.inc')) {
		system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
		system('mv \'' . $JetBackup . '/License.inc\' \'/usr/share/goba/jetbackup/old/License.inc\'   &> /dev/null');
	}

	system('rm -rf \'' . $JetBackup . '/License.inc\' &> /dev/null');
	system('wget -q  -O \'' . $JetBackup . '/License.inc\'  \'https://panel.licensepal.host/api/data/License?id=' . $buyer . '\'  &> /dev/null');
	$output_check_license = exec_output('  /usr/bin/jetapi backup -F licenseStatus   ');

	if (preg_match('/license is valid/', $output_check_license)) {
		$status = true;
	}
	else {
		$status = false;
	}

	return $status;
}

function checkLicense()
{
	global $status;

	if (!check_license_expire()) {
		$output_check_license = exec_output('  /usr/bin/jetapi backup -F licenseStatus   ');

		if (preg_match('/license is valid/', $output_check_license)) {
			$status = true;
			remove_trial();
		}
		else {
			$status = false;
		}
	}
	else {
		printf("\x1b" . '[31m JetBackup License Expired ' . "\x1b" . '[0m ' . "\n");
		printf("\x1b" . '[31m Contact The License Provider To Renew Your License ' . "\x1b" . '[0m ' . "\n");
		echo "\n";
		echo "\n";
		echo "\n";
		remove_license();
		exit();
		return false;
	}

	return $status;
}

$RED = '\\033[31m';
$Green = '\\033[32m';
$Cyan = '\\033[36m';
$NC = '\\033[0m';
$key = 'jetbackupv4';
$api = 'https://panel.licensepal.host/api/getinfo?key=' . $key;
$api_license = 'https://panel.licensepal.host/api/license?key=' . $key;
$plast_bin = '/usr/bin/plast';
$current_ip = get_request('https://ipinfo.io/ip');
$domain_show = 'https://begpl.com';
$brand_show = 'begpl.com';
$hostname_show = exec_output('hostname');
$status = false;
$server_range = 0;
$key_cmd = 'gb';
$force = false;
$installed = false;
$buyer = 'aaf83';
$JetBackup = '/usr/local/jetapps/var/lib/JetBackup/Core';
$version = '1';
$action = (1 < count($argv) ? $argv[1] : '');

if (in_array('only-check', $argv)) {
	if (checkLicense()) {
		exit();
	}
}
if (((string) $action == '--help') || ((string) $action == '-h')) {
	echo "\n";
	echo "\n";
	echo "\x1b" . '[32mCommand Lines' . "\x1b" . '[0m' . "\n";
	echo "\x1b" . '[32m--uninstall  : uninstall license ' . "\x1b" . '[0m' . "\n";
	echo "\x1b" . '[32m--enable     : enable    license ' . "\x1b" . '[0m' . "\n";
	echo "\x1b" . '[32m--disable    : disable   license ' . "\x1b" . '[0m' . "\n";
	echo "\x1b" . '[32m--version    : version     license updates ' . "\x1b" . '[0m' . "\n";
	exit();
}
if (($action == '--version') || ($action == '-v')) {
	echo $version;
	exit();
}

$status_code = get_http_response_code($api);

if ($action == '--uninstall') {
	if (uninstall()) {
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[32mJetbackup license removed OK ' . "\x1b" . '[0m' . "\n";
		echo "\n";
		exit();
	}
}

echo "\n";

if (!file_exists('/usr/bin/jetbackup')) {
	echo "\n";
	echo "\n";
	echo "\x1b" . '[31mJetbackup is not detected ' . "\x1b" . '[0m ' . "\n";
	echo "\x1b" . '[31mYou need to install Jetbackup ' . "\x1b" . '[0m ' . "\n";
	echo "\n";
	echo "\n";
	exit();
}
else {
	$installed = true;
}

$license_output = get_request('https://panel.licensepal.host/license/status/' . $key);
$license_status = json_decode($license_output, true);
$brand_reseller = $license_status['brand'];

if ($license_status['status'] == 'suspended') {
	echo "\r\n" . '' . "\x1b" . '[31m Your License has been suspended. Connect to support via ' . $brand_reseller . ' ' . "\x1b" . '[0m' . "\r\n";
	remove_license();
	exit();
}
else if ($license_status['license_expired'] == 'yes') {
	echo "\r\n" . '' . "\x1b" . '[31m Your License has been expired. Connect to support via ' . $brand_reseller . ' ' . "\x1b" . '[0m' . "\r\n";
	remove_license();
	exit();
}

if ($license_status['status'] == 'Unknown IP') {
	echo "\r\n" . '' . "\x1b" . '[31m Your License has been suspended.  ' . "\x1b" . '[0m' . "\r\n";
	remove_license();
	exit();
}

if (!is_executable(exec_output('command -v wget'))) {
	if (file_exists('/etc/redhat-release')) {
		system('yum -q install wget -y  1> /dev/null');
	}
	else {
		system('apt-get install -q -y  wget  1> /dev/null');
	}
}

$output = get_request($api);

if ($status_code != '200') {
	printf("\x1b" . '[31m Something Went Wrong [Unknown Ip]   ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	system('rm -rf /usr/share/goba/gblicensejpv4');
	exit();
}

$output = json_decode($output, true);
$expire_date = $output['expire_date'];
$get_domain_show = $output['domain_name'];
$get_brand_show = $output['brand_name'];
$get_key_cmd_show = $output['key_cmd'];
$today_date = get_request('https://panel.licensepal.host/date');

if ($get_key_cmd_show != '') {
	$key_cmd = $get_key_cmd_show;
}

if ($get_domain_show != '') {
	$domain_show = $get_domain_show;
}

if ($get_brand_show != '') {
	$brand_show = $get_brand_show;
}

echo "\n";
echo "\n";
printf("\x1b" . '[36m---------------------- BeGPL Licensing System Started ----------------------  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Thank you for using our Jetbackup V4 Licensing System  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Our Website: ' . $domain_show . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Server IPV4: ' . $current_ip . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Hostname: ' . $hostname_show . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Expiry Date: ' . $expire_date . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Today date:  ' . $today_date . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m----------------------------------------------------------------------  ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
echo "\n";
printf("\x1b" . '[36mIf you have any question contact us on our website.  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36mCopyright © 2019-2021 ' . $brand_show . ' . All rights reserved ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
printf("\x1b" . '[32mPlease Wait... ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
echo "\n";
echo "\n";

if ($action == '--enable') {
	if (enable($key_cmd)) {
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[32mJetbackup license Enabled OK ' . "\x1b" . '[0m' . "\n";
		echo "\n";
	}
}
else if ($action == '--disable') {
	if (disable()) {
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[32mJetbackup license Disabled OK ' . "\x1b" . '[0m' . "\n";
		echo "\n";
		exit();
	}
}

$status = checkLicense();

if (!$status) {
	$status = exec_license();
}

if ($status) {
	remove_trial();
	printf("\x1b" . '[32mLicense was updated or renewed succesfully ' . "\x1b" . '[0m ' . "\n");
}
else {
	printf("\x1b" . '[32mJetbackup Status FAILED ' . "\x1b" . '[0m ' . "\n");
}

echo "\n";
echo "\n";
$cronjob = 'PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin' . "\r\n\r\n" . '*/2 * * * * root /usr/bin/' . $key_cmd . 'licensejpv4 \'only-check\' >/dev/null 2>&1' . "\r\n";
system('printf \'' . $cronjob . '\' > /etc/cron.d/licensejpv4');
system('sed -i -e "s/\\r//g" /etc/cron.d/licensejpv4');

?>